"""
面板可见性测试工具 - 测试快捷键面板的显示和隐藏功能
"""

import sys
from PyQt5.QtWidgets import QApplication, QPushButton, QVBoxLayout, QLabel, QWidget
from PyQt5.QtCore import Qt
from app_launcher_fluent import ShortcutsPanel, AppLauncherFluent

class PanelVisibilityTest(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("面板显示/隐藏测试")
        self.resize(300, 200)
        
        # 创建布局
        layout = QVBoxLayout(self)
        
        # 创建标签和按钮
        self.status_label = QLabel("面板状态: 未创建")
        layout.addWidget(self.status_label)
        
        create_btn = QPushButton("创建面板")
        create_btn.clicked.connect(self.create_panel)
        layout.addWidget(create_btn)
        
        show_btn = QPushButton("显示面板")
        show_btn.clicked.connect(self.show_panel)
        layout.addWidget(show_btn)
        
        hide_btn = QPushButton("隐藏面板")
        hide_btn.clicked.connect(self.hide_panel)
        layout.addWidget(hide_btn)
        
        toggle_btn = QPushButton("切换面板")
        toggle_btn.clicked.connect(self.toggle_panel)
        layout.addWidget(toggle_btn)
        
        test_app_btn = QPushButton("测试完整应用")
        test_app_btn.clicked.connect(self.test_app)
        layout.addWidget(test_app_btn)
        
        # 面板引用
        self.panel = None
        self.app_launcher = None
    
    def create_panel(self):
        """创建面板"""
        try:
            self.panel = ShortcutsPanel()
            self.status_label.setText("面板状态: 已创建但未显示")
        except Exception as e:
            self.status_label.setText(f"面板创建失败: {e}")
            import traceback
            traceback.print_exc()
    
    def show_panel(self):
        """显示面板"""
        if not self.panel:
            self.create_panel()
            
        try:
            if self.panel:
                self.panel.show()
                self.panel.raise_()
                self.panel.activateWindow()
                self.status_label.setText("面板状态: 显示")
        except Exception as e:
            self.status_label.setText(f"面板显示失败: {e}")
            import traceback
            traceback.print_exc()
    
    def hide_panel(self):
        """隐藏面板"""
        if self.panel:
            try:
                self.panel.hide()
                self.status_label.setText("面板状态: 隐藏")
            except Exception as e:
                self.status_label.setText(f"面板隐藏失败: {e}")
                import traceback
                traceback.print_exc()
    
    def toggle_panel(self):
        """切换面板状态"""
        if not self.panel:
            self.create_panel()
            self.show_panel()
            return
            
        if self.panel.isVisible():
            self.hide_panel()
        else:
            self.show_panel()
    
    def test_app(self):
        """测试完整应用"""
        try:
            if not self.app_launcher:
                self.app_launcher = AppLauncherFluent()
                self.app_launcher.show()
            
            # 手动触发切换
            self.app_launcher.toggleShortcutsPanel()
            
            if hasattr(self.app_launcher, 'shortcutsPanel'):
                visible = self.app_launcher.shortcutsPanel.isVisible()
                self.status_label.setText(f"应用面板状态: {'可见' if visible else '隐藏'}")
        except Exception as e:
            self.status_label.setText(f"应用测试失败: {e}")
            import traceback
            traceback.print_exc()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = PanelVisibilityTest()
    window.show()
    sys.exit(app.exec_())
